package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.PaperprojectMapper;
import com.hbnu.system.model.entity.*;
import com.hbnu.system.service.*;
import com.hbnu.system.utils.CheckRoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class PaperprojectServiceImpl extends ServiceImpl<PaperprojectMapper, Paperproject> implements IPaperprojectService {

    @Autowired
    ITranproject_registrationService iTranproject_registrationService;

    @Autowired
    IPorproject_registrationService iPorproject_registrationService;

    @Autowired
    private IPaperService iPaperService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 添加项目
     * @param paperproject 要添加的论文项目
     * @return 新增操作是否成功
     */
    @Override
    public boolean addPaperProject(Paperproject paperproject) {
        // 拦截普通用户在非法状态修改的操作
        Paper paper = iPaperService.getOne(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getPaperID, paperproject.getPaperID()));
        checkRoleUtil.checkPermissionAchievement(paper.getStatus());

        if(StrUtil.isEmpty(paperproject.getProjectName()) || StrUtil.isBlank(paperproject.getProjectName())){
            throw new BaseException("请输入项目名称!");
        }

        Porproject_registration porproject = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectName, paperproject.getProjectName()));

        Tranproject_registration tranproject = iTranproject_registrationService.getOne(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectName, paperproject.getProjectName()));

        if(Objects.nonNull(porproject)){
            paperproject.setHead(porproject.getHead());
            paperproject.setProjectID(porproject.getProjectId());
            if(Objects.nonNull(paperproject.getStartTime())){
                paperproject.setStartTime(porproject.getStartTime());
            }
            if(Objects.nonNull(paperproject.getEndTime())){
                paperproject.setEndTime(porproject.getEndTime());
            }
        } else if(Objects.nonNull(tranproject)){
            paperproject.setHead(tranproject.getHead());
            paperproject.setProjectID(tranproject.getProjectId());
            if(Objects.nonNull(paperproject.getStartTime())){
                paperproject.setStartTime(paperproject.getStartTime());
            }
            if(Objects.nonNull(paperproject.getEndTime())){
                paperproject.setEndTime(paperproject.getEndTime());
            }
        } else {
            throw new BaseException("项目不存在!");
        }

        // 判断该项目是否已经添加过在该论文中
        List<Paperproject> paperprojectList = query().eq("projectName", paperproject.getProjectName())
                .eq("PaperID", paperproject.getPaperID()).list();
        if(!paperprojectList.isEmpty()){
            throw new BaseException("该项目已经添加过在该论文中!");
        }

        return baseMapper.insert(paperproject) == 1;
    }

    /**
     * 删除论文项目
     * @param paperProjectID 论文项目编号
     * @return 删除结果
     */
    @Override
    public boolean removePaperProject(String paperProjectID) {
        Paperproject paperproject = super.getOne(Wrappers.lambdaQuery(Paperproject.class)
                .eq(Paperproject::getPaperProjectID, paperProjectID));
        if(Objects.isNull(paperproject)){
            throw new BaseException("不存在该关联项目");
        }

        // 拦截普通用户在非法状态修改的操作
        Paper paper = iPaperService.getOne(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getPaperID, paperproject.getPaperID()));
        checkRoleUtil.checkPermissionAchievement(paper.getStatus());

        return baseMapper.deleteById(paperproject) == 1;
    }

    /**
     * 修改论文项目
     * @param paperproject 要修改的论文项目
     * @return 修改操作是否成功
     */
    @Transactional
    @Override
    public boolean updatePaperProject(Paperproject paperproject) {
        Paper paper = iPaperService.getOne(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getPaperID, paperproject.getPaperID()));
        checkRoleUtil.checkPermissionAchievement(paper.getStatus());

        // 删除之前的项目
        removeById(paperproject.getPaperProjectID());

        // 新增当前项目
        addPaperProject(paperproject);

        return true;
    }

    /**
     * 根据论文编号查询
     * @param paperID 论文编号
     * @return 关联项目列表
     */
    @Override
    public List<Paperproject> getPaperProjectByPaperID(String paperID) {
        Paper byId = iPaperService.getById(paperID);
        if (Objects.isNull(byId)){
            throw new BaseException("输入的论文编号有误!");
        }
        List<Paperproject> paperprojects = baseMapper.selectList(Wrappers.lambdaQuery(Paperproject.class)
                .eq(Paperproject::getPaperID, paperID));
        // 将 head 的 id 转为名称
        for (Paperproject paperproject : paperprojects) {
            String head = paperproject.getHead();
            User user = iUserService.getById(head);
            paperproject.setHead(user.getName());
        }
        return paperprojects;
    }
}